// Camera
float4x4 g_mWorld;
float4x4 g_mView;
float4x4 g_mProjection;

// Textures
texture g_Texture;

// Samplers
sampler g_Sampler = sampler_state
{
    Texture = g_Texture;
	AddressU = Wrap;
	AddressV = Wrap;
};

struct VertexShaderInput
{
    float4 vPosition : POSITION0;
    float2 vTextureCoordinate : TEXCOORD0;
};

struct VertexShaderOutput
{
    float4 vPosition : POSITION0;
    float2 vTextureCoordinate : TEXCOORD0;
};

struct DepthVertexShaderOutput
{
    float4 vPosition : POSITION0;
	float fDepth : DEPTH0;
};

VertexShaderOutput DefaultVertexShader(VertexShaderInput _Input)
{
	// Create the output structure
    VertexShaderOutput Output;

	// Apply the world, view and, projection matrices to the position
	Output.vPosition = mul(_Input.vPosition, g_mWorld);
	Output.vPosition = mul(Output.vPosition, g_mView);
	Output.vPosition = mul(Output.vPosition, g_mProjection);

    // Copy the texture coordinates
    Output.vTextureCoordinate = _Input.vTextureCoordinate;

	// Return the output structure
    return Output;
}


float4 DefaultPixelShader(VertexShaderOutput _Input) : COLOR0
{
	// Do a simple texture lookup
	return tex2D(g_Sampler, _Input.vTextureCoordinate);
}

DepthVertexShaderOutput DepthVertexShader(VertexShaderInput _Input)
{
	DepthVertexShaderOutput Output;

	// Transform the vertex position into view space
	Output.vPosition = mul(_Input.vPosition, g_mWorld);
	Output.vPosition = mul(Output.vPosition, g_mView);

	// Calculate the depth and scale the range
	Output.fDepth = -Output.vPosition.z / 2000.0f;

	// Transform the vertex position into screen space
	Output.vPosition = mul(Output.vPosition, g_mProjection);

	return Output;
}

float4 DepthPixelShader(DepthVertexShaderOutput _Input) : COLOR0
{
	return float4(_Input.fDepth, _Input.fDepth, _Input.fDepth, _Input.fDepth);
}

// Render things
technique Default
{
    pass Pass1
    {
        VertexShader = compile vs_3_0 DefaultVertexShader();
        PixelShader = compile ps_3_0 DefaultPixelShader();
    }
}

technique Depth
{
    pass Pass1
    {
        VertexShader = compile vs_3_0 DepthVertexShader();
        PixelShader = compile ps_3_0 DepthPixelShader();
    }
}